package com.example.theorygame.layer;

import android.view.MotionEvent;

import com.example.theorygame.util.CommouUtils;

import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;

/**
 * 一次大赛页面
 */
public class FourthLayer extends BaseLayer {
    /**
     * 变量定义区域
     */
    //1白色背景
    private CCSprite bj;
    //2.五个人物精灵
    private CCSprite blue, prople, pink, yellow, brown;
    //3.坐标值
    private float x1, x2, x3, x4, x5, y1, y2, y3, y4, y5;
    //4.线条
    private String line1 = "image/layer4/line1";
    private String line2 = "image/layer4/line2";
    private String line3 = "image/layer4/line3";
    private String line4 = "image/layer4/line4";
    private CCSprite blue_prople, blue_pink, blue_yellow, blue_brown;
    private CCSprite prople_pink, prople_yellow, prople_brown;
    private CCSprite pink_yellow, pink_brown;
    private CCSprite yellow_brown;
    //5.定义得分标签
    private CCLabel s1, s2, s3, s4, s5;
    //6.定义文字标签
    private CCLabel t1, t2, t3, t4;
    //7.定义人物按钮
    private CCSprite b1, b2, b3, b4, b5;
    //8.定义你选择的人物      1,2,3,4,5
    private int youchose = 0;
    private String name;
    //9.定义新按钮第一局
    private CCSprite firstButton;
    //10.定义第一局触摸锁
    private int rook = 0;
    //11.定义按键次数
    private int click = 0;
    //12.定义结果精灵
    private CCSprite a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
    private String str;
    //13.定义下一局按钮
    private CCSprite nextButton;
    //14.定义是否选对1,0
    private int trueClock;
    //15.定义切换场景按钮
    private CCSprite nextLayer;
    //16.定义总结界面文字标签
    private CCLabel lab1, lab2, lab3, lab4, lab5, lab6, lab7, lab8, lab9;

    /**
     * 构造方法区域
     */
    public FourthLayer() {
        //白色背景
        bj = CCSprite.sprite("image/bg.png");
        bj.setAnchorPoint(0, 0);
        bj.setPosition(0, 0);
        this.addChild(bj, 0);
        //定义位置数据
        x1 = winSize.width / 4 - 15;
        y1 = winSize.height / 2 + winSize.width / 4 - 27;
        x3 = x2 = winSize.width / 4 + winSize.width / 8 * 1.7f - 15;
        y5 = y2 = winSize.height / 2 + winSize.width / 8 - 27;
        y4 = y3 = winSize.height / 2 - winSize.width / 8 - 15;
        x5 = x4 = winSize.width / 2 - x2 - 27;
        //画线
        blue_prople = CCSprite.sprite("image/layer4/line1.png");
        yellow_brown = CCSprite.sprite("image/layer4/line1.png");
        pink_yellow = CCSprite.sprite("image/layer4/line1.png");
        prople_pink = CCSprite.sprite("image/layer4/line1.png");
        blue_brown = CCSprite.sprite("image/layer4/line1.png");

        blue_prople.setAnchorPoint(0, 0);
        blue_prople.setPosition(132, 160);

        blue_brown.setAnchorPoint(1, 0);
        blue_brown.setFlipX(true);
        blue_brown.setPosition(132, 160);

        prople_pink.setAnchorPoint(0, 0);
        prople_pink.setPosition(132, 160);
        prople_pink.setRotation(72f);

        pink_yellow.setAnchorPoint(0, 0);
        pink_yellow.setPosition(132, 160);
        pink_yellow.setRotation(144f);

        yellow_brown.setAnchorPoint(0, 0);
        yellow_brown.setPosition(132, 160);
        yellow_brown.setRotation(216f);
        this.addChild(blue_brown);
        this.addChild(blue_prople);
        this.addChild(prople_pink);
        this.addChild(pink_yellow);
        this.addChild(yellow_brown);

        //复读机blue
        blue = CCSprite.sprite("image/layer4/tournament_peep_1.png");
        blue.setAnchorPoint(0, 0);
        blue.setPosition(115, 239);
        blue.setFlipX(true);
        this.addChild(blue, 2);

        //老油条prople
        prople = CCSprite.sprite("image/layer4/tournament_peep_2.png");
        prople.setAnchorPoint(0, 0);
        prople.setPosition(221, 161);
        prople.setFlipX(true);
        this.addChild(prople, 2);
        //小粉红pink
        pink = CCSprite.sprite("image/layer4/tournament_peep_3.png");
        pink.setAnchorPoint(0, 0);
        pink.setPosition(180, 37);
        pink.setFlipX(true);
        this.addChild(pink, 2);
        //黑帮老铁yellow
        yellow = CCSprite.sprite("image/layer4/tournament_peep_4.png");
        yellow.setAnchorPoint(0, 0);
        yellow.setPosition(51, 37);
        this.addChild(yellow, 2);
        //佛耳墨斯brown
        brown = CCSprite.sprite("image/layer4/tournament_peep_5.png");
        brown.setAnchorPoint(0, 0);
        brown.setPosition(12, 160);
        this.addChild(brown, 2);
        //在人物下面添加得分标签
        s1 = CCLabel.makeLabel(" ", "圆体", 15);
        s1.setAnchorPoint(0, 0);
        s1.setColor(ccc3(0, 0, 0));
        s1.setPosition(134, 220);
        s2 = CCLabel.makeLabel(" ", "圆体", 15);
        s2.setAnchorPoint(0, 0);
        s2.setColor(ccc3(0, 0, 0));
        s2.setPosition(200, 181);
        s3 = CCLabel.makeLabel(" ", "圆体", 15);
        s3.setAnchorPoint(0, 0);
        s3.setColor(ccc3(0, 0, 0));
        s3.setPosition(177, 90);
        s4 = CCLabel.makeLabel(" ", "圆体", 15);
        s4.setAnchorPoint(0, 0);
        s4.setColor(ccc3(0, 0, 0));
        s4.setPosition(83, 94);
        s5 = CCLabel.makeLabel(" ", "圆体", 15);
        s5.setAnchorPoint(0, 0);
        s5.setColor(ccc3(0, 0, 0));
        s5.setPosition(57, 187);
        this.addChild(s1);
        this.addChild(s2);
        this.addChild(s3);
        this.addChild(s4);
        this.addChild(s5);
        //添加文字
        t1 = CCLabel.labelWithString("是时候上赛场啦！各个角色会相互之间会两两配", "圆体", 10);
        t2 = CCLabel.labelWithString("对，进行十局比赛，每一局比赛分成十轮。", "圆体", 10);
        t3 = CCLabel.labelWithString("你觉得哪个角色总分会最高，能够笑到最后呢？ ", "圆体", 10);
        t4 = CCLabel.labelWithString("仔细想清楚，然后...请做出你的选择：", "圆体", 10);
        t1.setAnchorPoint(0, 0);
        t2.setAnchorPoint(0, 0);
        t3.setAnchorPoint(0, 0);
        t4.setAnchorPoint(0, 0);
        t1.setColor(ccc3(0, 0, 0));
        t2.setColor(ccc3(0, 0, 0));
        t3.setColor(ccc3(0, 0, 0));
        t4.setColor(ccc3(0, 0, 0));
        t1.setPosition(260, 298);
        t2.setPosition(260, 278);
        t3.setPosition(260, 248);
        t4.setPosition(260, 228);
        this.addChild(t1);
        this.addChild(t2);
        this.addChild(t3);
        this.addChild(t4);
        //添加精灵选择按钮
        b1 = CCSprite.sprite("image/layer4/sprite1.png");
        b2 = CCSprite.sprite("image/layer4/sprite2.png");
        b3 = CCSprite.sprite("image/layer4/sprite3.png");
        b4 = CCSprite.sprite("image/layer4/sprite4.png");
        b5 = CCSprite.sprite("image/layer4/sprite5.png");
        b1.setAnchorPoint(0, 0);
        b2.setAnchorPoint(0, 0);
        b3.setAnchorPoint(0, 0);
        b4.setAnchorPoint(0, 0);
        b5.setAnchorPoint(0, 0);
        b1.setScale(0.6);
        b2.setScale(0.6);
        b3.setScale(0.6);
        b4.setScale(0.6);
        b5.setScale(0.6);
        b1.setPosition(260, 170);
        b2.setPosition(260, 130);
        b3.setPosition(260, 90);
        b4.setPosition(260, 50);
        b5.setPosition(260, 10);
        this.addChild(b1);
        this.addChild(b2);
        this.addChild(b3);
        this.addChild(b4);
        this.addChild(b5);

        //开启触摸
        setIsTouchEnabled(true);
    }

    /**
     * 方法定义区域
     */
    //进入选择界面方法
    public void chosefirst() {
        //修改文字
        t1.setString("好，你的选择是" + name + "！火车不是吹的，牛不");
        t2.setString("是推的，是骡子是狗牵出来瞅瞅，咱们一个一个");
        t3.setString("来，先看看第一局比赛的战况如何...");
        t3.setPosition(260, 258);
        //清除按钮
        t4.removeFromParentAndCleanup(true);
        b1.removeFromParentAndCleanup(true);
        b2.removeFromParentAndCleanup(true);
        b3.removeFromParentAndCleanup(true);
        b4.removeFromParentAndCleanup(true);
        b5.removeFromParentAndCleanup(true);
        //添加新按钮
        firstButton = CCSprite.sprite("image/layer4/first.png");
        firstButton.setAnchorPoint(0, 0);
        firstButton.setPosition(260, 160);
        this.addChild(firstButton);
        youchose = 1;
    }

    //定义修改结果方法
    public void changeRes(int i, String name1, String name2, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int s1, int s2) {
        t1.setString("第" + i + "局：" + name1 + "对战" + name2);
        t2.setString("每轮战况：总分：" + s1 + "比" + s2);
        t3.removeFromParentAndCleanup(true);
        this.a1 = CCSprite.sprite(getPath(a1));
        this.a2 = CCSprite.sprite(getPath(a2));
        this.a3 = CCSprite.sprite(getPath(a3));
        this.a4 = CCSprite.sprite(getPath(a4));
        this.a5 = CCSprite.sprite(getPath(a5));
        this.a6 = CCSprite.sprite(getPath(a6));
        this.a7 = CCSprite.sprite(getPath(a7));
        this.a8 = CCSprite.sprite(getPath(a8));
        this.a9 = CCSprite.sprite(getPath(a9));
        this.a10 = CCSprite.sprite(getPath(a10));
        this.a1.setAnchorPoint(0, 0);
        this.a2.setAnchorPoint(0, 0);
        this.a3.setAnchorPoint(0, 0);
        this.a4.setAnchorPoint(0, 0);
        this.a5.setAnchorPoint(0, 0);
        this.a6.setAnchorPoint(0, 0);
        this.a7.setAnchorPoint(0, 0);
        this.a8.setAnchorPoint(0, 0);
        this.a9.setAnchorPoint(0, 0);
        this.a10.setAnchorPoint(0, 0);
        this.a1.setPosition(260, 238);
        this.a2.setPosition(290, 238);
        this.a3.setPosition(320, 238);
        this.a4.setPosition(350, 238);
        this.a5.setPosition(380, 238);
        this.a6.setPosition(260, 198);
        this.a7.setPosition(290, 198);
        this.a8.setPosition(320, 198);
        this.a9.setPosition(350, 198);
        this.a10.setPosition(380, 198);
        this.addChild(this.a1);
        this.addChild(this.a2);
        this.addChild(this.a3);
        this.addChild(this.a4);
        this.addChild(this.a5);
        this.addChild(this.a6);
        this.addChild(this.a7);
        this.addChild(this.a8);
        this.addChild(this.a9);
        this.addChild(this.a10);
        //修改得分数
        switch (i) {
            case 1:
                this.s1.setString("-1");
                this.s2.setString("3");
                break;
            case 2:
                this.s1.setString("19");
                this.s3.setString("20");
                break;
            case 3:
                this.s1.setString("39");
                this.s4.setString("20");
                break;
            case 4:
                this.s1.setString("57");
                this.s5.setString("18");
                break;
            case 5:
                this.s2.setString("33");
                this.s3.setString("10");
                break;
            case 6:
                this.s2.setString("36");
                this.s4.setString("19");
                break;
            case 7:
                this.s2.setString("45");
                this.s5.setString("15");
                break;
            case 8:
                this.s3.setString("30");
                this.s4.setString("39");
                break;
            case 9:
                this.s3.setString("29");
                this.s5.setString("42");
                break;
            case 10:
                this.s4.setString("46");
                this.s5.setString("45");
                break;
            default:
                break;
        }
        //移除第一局按钮
        firstButton.removeFromParentAndCleanup(true);
        //下一局按钮
        nextButton = CCSprite.sprite("image/layer4/next.png");
        nextButton.setAnchorPoint(0, 0);
        nextButton.setScale(0.6f);
        nextButton.setPosition(260, 10);
        this.addChild(nextButton);
    }

    //定义结果精灵方法  0 00 1 13 2 22 3 31
    public String getPath(int i) {
        if (i == 0) {
            str = "image/layer4/score_small_4.png";
        }
        if (i == 1) {
            str = "image/layer4/score_small_3.png";
        }
        if (i == 2) {
            str = "image/layer4/score_small_1.png";
        }
        if (i == 3) {
            str = "image/layer4/score_small_2.png";
        }
        return str;
    }

    //总结界面
    public void lastPanel() {
        if (trueClock == 0) {
            t1.setString("复读机！（对不住啦，" + name + "。）");
        } else {
            t1.setString("恭喜你!你选择的答案是对的");
        }
        t2.removeFromParentAndCleanup(true);
        a1.removeFromParentAndCleanup(true);
        a2.removeFromParentAndCleanup(true);
        a3.removeFromParentAndCleanup(true);
        a4.removeFromParentAndCleanup(true);
        a5.removeFromParentAndCleanup(true);
        a6.removeFromParentAndCleanup(true);
        a7.removeFromParentAndCleanup(true);
        a8.removeFromParentAndCleanup(true);
        a9.removeFromParentAndCleanup(true);
        a10.removeFromParentAndCleanup(true);
        nextButton.removeFromParentAndCleanup(true);
        lab1 = CCLabel.labelWithString("复读机也有很多别名。像什么黄金准则啦，互惠利", "圆体", 10);
        lab2 = CCLabel.labelWithString("他主义啦，以牙还牙啦，或者说互惠宽容。这就是", "圆体", 10);
        lab3 = CCLabel.labelWithString("为什么「和平」会如雨后春笋一般在一战的战壕中", "圆体", 10);
        lab4 = CCLabel.labelWithString("蔓延开来：当你被迫每天与同一批人——不是仅仅", "圆体", 10);
        lab5 = CCLabel.labelWithString("是普遍意义上的「敌人」而是同样的一批人——进", "圆体", 10);
        lab6 = CCLabel.labelWithString("行同样的游戏（例如战争）时，复读机这样的人不", "圆体", 10);
        lab7 = CCLabel.labelWithString("单单会赢得一场战役，他们会赢得整场战争。", "圆体", 10);

        lab8 = CCLabel.labelWithString("但是，如果同样的游戏每玩一场，情况都改变一", "圆体", 10);
        lab9 = CCLabel.labelWithString("次。设想一下，假如我们进行多场这样大赛...", "圆体", 10);
        lab1.setColor(ccc3(0, 0, 0));
        lab2.setColor(ccc3(0, 0, 0));
        lab3.setColor(ccc3(0, 0, 0));
        lab4.setColor(ccc3(0, 0, 0));
        lab5.setColor(ccc3(0, 0, 0));
        lab6.setColor(ccc3(0, 0, 0));
        lab7.setColor(ccc3(0, 0, 0));
        lab8.setColor(ccc3(0, 0, 0));
        lab9.setColor(ccc3(0, 0, 0));
        lab1.setAnchorPoint(0, 0);
        lab2.setAnchorPoint(0, 0);
        lab3.setAnchorPoint(0, 0);
        lab4.setAnchorPoint(0, 0);
        lab5.setAnchorPoint(0, 0);
        lab6.setAnchorPoint(0, 0);
        lab7.setAnchorPoint(0, 0);
        lab8.setAnchorPoint(0, 0);
        lab9.setAnchorPoint(0, 0);
        lab1.setPosition(260, 268);
        lab2.setPosition(260, 248);
        lab3.setPosition(260, 228);
        lab4.setPosition(260, 208);
        lab5.setPosition(260, 198);
        lab6.setPosition(260, 178);
        lab7.setPosition(260, 158);
        lab8.setPosition(260, 120);
        lab9.setPosition(260, 100);
        this.addChild(lab1);
        this.addChild(lab2);
        this.addChild(lab3);
        this.addChild(lab4);
        this.addChild(lab5);
        this.addChild(lab6);
        this.addChild(lab7);
        this.addChild(lab8);
        this.addChild(lab9);
        nextLayer = CCSprite.sprite("image/layer4/nextLayer.png");
        nextLayer.setAnchorPoint(0, 0);
        nextLayer.setPosition(250, 10);
        nextLayer.setScale(0.8f);
        this.addChild(nextLayer);
    }

    /**
     * 触摸事件区域
     */
    @Override
    public boolean ccTouchesBegan(MotionEvent event) {
        CGPoint conver = this.convertTouchToNodeSpace(event);
        //下一局按钮
        if (rook == 1 && click > 0) {
            if (CGRect.containsPoint(nextButton.getBoundingBox(), conver)) {
                click += 1;
                switch (click) {
                    case 2:
                        changeRes(2, "复读机", "万年小粉红", 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 20, 20);
                        break;
                    case 3:
                        changeRes(3, "复读机", "黑帮老铁", 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 20, 20);
                        break;
                    case 4:
                        changeRes(4, "复读机", "福尔摩星儿", 2, 1, 3, 2, 2, 2, 2, 2, 2, 2, 18, 18);
                        break;
                    case 5:
                        changeRes(5, "千年老油条", "万年小粉红", 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 30, -10);
                        break;
                    case 6:
                        changeRes(6, "千年老油条", "黑帮老铁", 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, -1);
                        break;
                    case 7:
                        changeRes(7, "千年老油条", "福尔摩星儿", 3, 0, 3, 3, 0, 0, 0, 0, 0, 0, 9, 3);
                        break;
                    case 8:
                        changeRes(8, "万年小粉红", "黑帮老铁", 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 20, 20);
                        break;
                    case 9:
                        changeRes(9, "万年小粉红", "福尔摩星儿", 2, 1, 2, 2, 1, 1, 1, 1, 1, 1, -1, 27);
                        break;
                    case 10:
                        changeRes(10, "黑帮老铁", "福尔摩星儿", 2, 1, 3, 3, 0, 0, 0, 0, 0, 0, 7, 3);
                        break;
                    case 11:
                        lastPanel();
                        ;
                        break;
                }

            }
        }
        //第一局按钮
        if (youchose != 0 && rook == 0) {
            if (CGRect.containsPoint(firstButton.getBoundingBox(), conver)) {
                click = 1;
                changeRes(1, "复读机", "千年老油条", 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 3);
                rook = 1;
            }
        }
        //选择人物按钮
        if (youchose == 0) {
            if (CGRect.containsPoint(b1.getBoundingBox(), conver)) {
                name = "复读机";
                chosefirst();
                trueClock = 1;
            }
            if (CGRect.containsPoint(b2.getBoundingBox(), conver)) {
                name = "万年小粉红";
                chosefirst();
                trueClock = 0;
            }
            if (CGRect.containsPoint(b3.getBoundingBox(), conver)) {
                name = "千年老油条";
                chosefirst();
                trueClock = 0;
            }
            if (CGRect.containsPoint(b4.getBoundingBox(), conver)) {
                name = "黑帮老铁";
                chosefirst();
                trueClock = 0;
            }
            if (CGRect.containsPoint(b5.getBoundingBox(), conver)) {
                name = "福尔摩星儿";
                chosefirst();
                trueClock = 0;
            }
        }
        //切换页面
        if (click > 11) {
            if (CGRect.containsPoint(nextLayer.getBoundingBox(), conver)) {
                CommouUtils.changeLayer(new FifthLayer());
            }
        }
        return true;

    }

}
